home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2008 January / Mac_easy_01_08.iso / Software / Online / Chat / macam.0.9.1.dmg / macam source / utilities / yuv2rgbCPIA422.c < prev    next >
Encoding:
C/C++ Source or Header  |  2006-03-03  |  6.6 KB  |  180 lines

  1. /*
  2.  macam - webcam app and QuickTime driver component
  3.  Copyright (C) 2002 Matthias Krauss (macam@matthias-krauss.de)
  4.  
  5.  This program is free software; you can redistribute it and/or modify
  6.  it under the terms of the GNU General Public License as published by
  7.  the Free Software Foundation; either version 2 of the License, or
  8.  (at your option) any later version.
  9.  
  10.  This program is distributed in the hope that it will be useful,
  11.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  GNU General Public License for more details.
  14.  
  15.  You should have received a copy of the GNU General Public License
  16.  along with this program; if not, write to the Free Software
  17.  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  $Id: yuv2rgbCPIA422.c,v 1.2 2006/03/03 18:32:36 hxr Exp $
  19. */
  20.  
  21. /*
  22.  
  23.  This file contains just a raw function body and is intended to be #included from other files, such as yuv2rgb.c
  24.  
  25.  The function prototype has to be:
  26.  
  27.  void <whatever>(int width, int height, unsigned char *src, unsigned char *dst, long srcRowExtra, long dstRowExtra);
  28.  
  29.  */
  30.  
  31. long x,y;                    //loop counters
  32.     long y1,y2,y3,y4;                //The y components in yuv
  33.     long u1,u1g,u1b,u2,u2g,u2b;            //The raw u components and the u differences
  34.     long v1,v1r,v1g,v2,v2r,v2g;            //The raw v components and the v differences
  35.     long r1,g1,b1,r2,g2,b2,r3,g3,b3,r4,g4,b4;    //Destination rgb
  36.     long dstRowBytes;                //EXCLUDING the extra part, just the raw data length per row
  37.     unsigned long ul1,ul2,ul3,ul4;         //Temp vars to access memory
  38. #ifdef YUV2RGB_ALPHA
  39.     short bpp=4;
  40. #else
  41.     short bpp=3;
  42. #pragma unused(ul4)
  43. #endif
  44.     width/=4;                    //We work in 4 x 1 blocks
  45.     dstRowBytes=4*bpp*width;
  46. #ifdef YUV2RGB_FLIP
  47.     dstRowExtra+=2*dstRowBytes;            //From beginning to end of next line
  48. #endif
  49.     for (y=height;y;y--) {
  50.         for (x=width;x;x--) {
  51. //Read from source buffer
  52.             ul1=CFSwapInt32HostToBig(*((unsigned  long*)(src))); src+=4;    //Read yuyv in line 1
  53.             ul2=CFSwapInt32HostToBig(*((unsigned  long*)(src))); src+=4;    //Read yuyv in line 1
  54. //Extract yuv pixel data
  55.             y1 =(ul1&0xff000000)>>16;
  56.             u1 =((ul1&0x00ff0000)>>16)-128;
  57.             y2 =(ul1&0x0000ff00);
  58.             v1 =(ul1&0x000000ff)-128;
  59.             y3 =(ul2&0xff000000)>>16;
  60.             u2 =((ul2&0x00ff0000)>>16)-128;
  61.             y4 =(ul2&0x0000ff00);
  62.             v2 =(ul2&0x000000ff)-128;
  63.             //convert yuv to rgb: calculate difference coefficients
  64.             u1g =u1*88;
  65.             u1b =u1*454;
  66.             v1r =v1*359;
  67.             v1g =v1*183;
  68.             u2g =u2*88;
  69.             u2b =u2*454;
  70.             v2r =v2*359;
  71.             v2g =v2*183;
  72.             //convert yuv to rgb: assemble rgb
  73.             r1=(y1+v1r)/256;
  74.             g1=(y1-u1g-v1g)/256;
  75.             b1=(y1+u1b)/256;
  76.             r2=(y2+v1r)/256;
  77.             g2=(y2-u1g-v1g)/256;
  78.             b2=(y2+u1b)/256;
  79.             r3=(y3+v2r)/256;
  80.             g3=(y3-u2g-v2g)/256;
  81.             b3=(y3+u2b)/256;
  82.             r4=(y4+v2r)/256;
  83.             g4=(y4-u2g-v2g)/256;
  84.             b4=(y4+u2b)/256;
  85.             //convert yuv to rgb: check value bounds
  86.             r1=CLAMP(r1,0,255);
  87.             g1=CLAMP(g1,0,255);
  88.             b1=CLAMP(b1,0,255);
  89.             r2=CLAMP(r2,0,255);
  90.             g2=CLAMP(g2,0,255);
  91.             b2=CLAMP(b2,0,255);
  92.             r3=CLAMP(r3,0,255);
  93.             g3=CLAMP(g3,0,255);
  94.             b3=CLAMP(b3,0,255);
  95.             r4=CLAMP(r4,0,255);
  96.             g4=CLAMP(g4,0,255);
  97.             b4=CLAMP(b4,0,255);
  98. //Assemble longs from rgb data
  99. #ifndef YUV2RGB_ALPHA
  100. #ifdef YUV2RGB_FLIP
  101. //3bpp, flipped assembly
  102.             ul3=(((unsigned long)r4)<<24)
  103.                 |(((unsigned long)g4)<<16)
  104.                 |(((unsigned long)b4)<<8)
  105.                 |(((unsigned long)r3));
  106.             ul2=(((unsigned long)g3)<<24)
  107.                 |(((unsigned long)b3)<<16)
  108.                 |(((unsigned long)r2)<<8)
  109.                 |(((unsigned long)g2));
  110.             ul1=(((unsigned long)b2)<<24)
  111.                 |(((unsigned long)r1)<<16)
  112.                 |(((unsigned long)g1)<<8)
  113.                 |(((unsigned long)b1));
  114. #else
  115. //3bpp, unflipped assembly
  116.             ul1=(((unsigned long)r1)<<24)
  117.                 |(((unsigned long)g1)<<16)
  118.                 |(((unsigned long)b1)<<8)
  119.                 |(((unsigned long)r2));
  120.             ul2=(((unsigned long)g2)<<24)
  121.                 |(((unsigned long)b2)<<16)
  122.                 |(((unsigned long)r3)<<8)
  123.                 |(((unsigned long)g3));
  124.             ul3=(((unsigned long)b3)<<24)
  125.                 |(((unsigned long)r4)<<16)
  126.                 |(((unsigned long)g4)<<8)
  127.                 |(((unsigned long)b4));
  128. #endif
  129. #else
  130. //4bpp assembly - no matter if flipped or unflipped
  131.             ul1=0xff000000
  132.                 |(((unsigned long)r1)<<16)
  133.                 |(((unsigned long)g1)<<8)
  134.                 |(((unsigned long)b1));
  135.             ul2=0xff000000
  136.                 |(((unsigned long)r2)<<16)
  137.                 |(((unsigned long)g2)<<8)
  138.                 |(((unsigned long)b2));
  139.             ul3=0xff000000
  140.                 |(((unsigned long)r3)<<16)
  141.                 |(((unsigned long)g3)<<8)
  142.                 |(((unsigned long)b3));
  143.             ul4=0xff000000
  144.                 |(((unsigned long)r4)<<16)
  145.                 |(((unsigned long)g4)<<8)
  146.                 |(((unsigned long)b4));
  147. #endif
  148. //Output to destination buffer
  149. #ifdef YUV2RGB_FLIP
  150. #ifdef YUV2RGB_ALPHA
  151.             dst-=16;
  152.             *((unsigned long*)(dst+12))=CFSwapInt32BigToHost(ul1);
  153.             *((unsigned long*)(dst+ 8))=CFSwapInt32BigToHost(ul2);
  154.             *((unsigned long*)(dst+ 4))=CFSwapInt32BigToHost(ul3);
  155.             *((unsigned long*)(dst   ))=CFSwapInt32BigToHost(ul4);
  156. #else    //YUV2RGB_ALPHA
  157.             dst-=12;
  158.             *((unsigned long*)(dst+ 8))=CFSwapInt32BigToHost(ul1);
  159.             *((unsigned long*)(dst+ 4))=CFSwapInt32BigToHost(ul2);
  160.             *((unsigned long*)(dst   ))=CFSwapInt32BigToHost(ul3);
  161. #endif    //YUV2RGB_ALPHA
  162. #else    //YUV2RGB_FLIP
  163. #ifdef YUV2RGB_ALPHA
  164.             *((unsigned long*)(dst))=CFSwapInt32BigToHost(ul1);
  165.             *((unsigned long*)(dst+4))=CFSwapInt32BigToHost(ul2);
  166.             *((unsigned long*)(dst+8))=CFSwapInt32BigToHost(ul3);
  167.             *((unsigned long*)(dst+12))=CFSwapInt32BigToHost(ul4);
  168.             dst+=16;
  169. #else    //YUV2RGB_ALPHA
  170.             *((unsigned long*)(dst))=CFSwapInt32BigToHost(ul1);
  171.             *((unsigned long*)(dst+4))=CFSwapInt32BigToHost(ul2);
  172.             *((unsigned long*)(dst+8))=CFSwapInt32BigToHost(ul3);
  173.             dst+=12;
  174. #endif    //YUV2RGB_ALPHA
  175. #endif    //YUV2RGB_FLIP
  176.         }
  177.         src+=srcRowExtra;
  178.         dst+=dstRowExtra;
  179.     }
  180. //End of included, preprocessor-customized code